package mars.ns.selectors

import mars.ns.core._
import dummy.world.Family

trait NodeSelection[T <: Node] {
	val MAX_RECURSION : Int = 100;
	var avoidRecursion : Int = 0;
    def selectNode(network: Network[T]): T = network.getNodes.head
    
    def connect(network: Network[T], node: T) :Set[Link] = {
    	var otherNode = network.getNodes.head
    	var tail = network.getNodes.tail
    	while(otherNode.equals(node) || node.getLinkedObjects.contains(otherNode)) {
    		otherNode = tail.head
    		tail = tail.tail
    		if(tail.size == 0) {
    			avoidRecursion += 1
				if(this.avoidRecursion == MAX_RECURSION){
					throw new Error("Couldn't connect two nodes in the resting network");
				}
    			return connect(network,selectNode(network))
    		}
    	}
    	this.avoidRecursion = 0
    	node.addDoubleLink(otherNode)
    	return node.getLinksTo(otherNode)
    }
}